﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.Utility;

namespace EnvironmentManagerSystem.EvaluationUnitManager
{
    public partial class EvaluationUnitManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                if (!(Boolean)Session["Admin"])
                {
                    gvInfo.Columns[gvInfo.Columns.Count - 1].Visible = false;
                    divInsert.Visible = false;
                }
                BindGv();
            }
        }

        private void BindGv()
        {
            gvInfo.DataSource = EvaluationUnit.GetList();
            gvInfo.DataBind();
        }

        protected void gvInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = (int)gvInfo.DataKeys[e.RowIndex].Value;
            if (!EvaluationUnit.DeleteUnit(ID))
                Jscript.Alert("删除失败！", this);
            BindGv();
        }

        protected void gvInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvInfo.PageIndex = e.NewPageIndex;
            BindGv();
        }

        protected void gvInfo_DataBound(object sender, EventArgs e)
        {
            OfficeHelper.HelpDv(gvInfo);
            foreach (GridViewRow row in gvInfo.Rows)
            {
                if (row.RowIndex != -1)
                    row.Cells[0].Text = (gvInfo.PageIndex * gvInfo.PageSize + row.RowIndex + 1).ToString();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            BindGv();
        }

        #region LinkButton1_Command(x)
        //启用状态
        protected void lbtnEnabled_Command(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandName);
            DataTable dt = EvaluationUnit.GetEnabled(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                string a = dt.Rows[0]["Enabled"].ToString();
                if (dt.Rows[0]["Enabled"].ToString() == "1")
                {
                    if (EvaluationUnit.UpdateEnabled2(id) == true)
                    {
                        Jscript.Alert("禁用成功!", this);
                    }
                }
                if (dt.Rows[0]["Enabled"].ToString() == "2")
                {
                    if (EvaluationUnit.UpdateEnabled1(id) == true)
                    {
                        Jscript.Alert("启用成功!", this);
                    }
                }
                Response.Redirect(Request.Url.ToString());
            }
        }
        #endregion
    }
}
